<template>
  <div>
    <el-dialog
      title="选择评审活动"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="600px"
      @closed="resetForm('form')"
    >
      <div>
        <el-form ref="form" :rules="rules" :model="form" label-width="80px">
          <el-form-item label="评审活动" prop="reviewId">
            <el-select v-model="form.reviewId" placeholder="请选择评审活动">
              <el-option
                v-for="v in courseData"
                :key="v.id"
                :label="v.title"
                :value="v.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="custom" v-preventReClick @click="submitForm('form')"
          >提 交</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { expertsReviewResourceAdd } from '@/api/admin/audit/expertsReviewResource.js'
import { expertsReviewQuery } from '@/api/admin/audit/expertsReview.js'
import Bus from '@/utils/bus.js'
export default {
  data() {
    return {
      dialogVisible: false,
      form: {
        resType: '1',
        reviewId: '', //评审会
        resourceList: [], //资源集合
      },
      rules: {
        reviewId: [
          { required: true, message: '请选择评审活动', trigger: 'blur' },
        ],
      },
      listQuery: {
        page: 1,
        limit: 1000,
      },
      courseData: [],
    }
  },
  mounted() {
    Bus.$on('showDialogPushReview', this.init)
  },
  beforeDestroy() {
    Bus.$off('showDialogPushReview')
  },
  methods: {
    //显示弹窗
    init(row) {
      this.form.resourceList.push(row)
      this.getReviewList(row)
    },
    getReviewList(row) {
      let deptId = row.tbSysDept.deptFullpath.split('/')[2]
      let isOther = row.tbSysDept.deptFullpath.split('/')[1]
      let ruleVO = {
        courseType: row.courseType,
        deptId: deptId,
      }
      this.$set(this.listQuery, 'isOther', isOther)
      this.$set(this.listQuery, 'ruleVO', ruleVO)
      expertsReviewQuery(this.listQuery).then((res) => {
        this.courseData = res.data.data
        if (!this.courseData.length) {
          this.$message({
            message: '暂无相关评审活动',
            type: 'warning',
          })
          return
        }
        this.dialogVisible = true
      })
    },
    submitForm(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.add()
        }
      })
    },
    //新增
    add() {
      expertsReviewResourceAdd(this.form).then((res) => {
        this.done(res)
      })
    },
    //编辑
    done(res) {
      if (!res || res.data.code != 0) return
      this.dialogVisible = false
      this.$message({
        message: '操作成功',
        type: 'success',
      })
    },
    //重置表单
    resetForm(form) {
      this.$refs[form].resetFields()
      this.form.reviewId = ''
      this.form.resourceList = []
      this.form.resType = '1'
    },
  },
}
</script>